package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.iptv.grape.bu.BuildRes;
import com.iptv.grape.net.Entity.LoginData;
import com.iptv.grape.net.Entity.LoginResult;
import com.iptv.grape.ui.frgmt.my.impls.SystemInfo;
import com.iptv.grape.ui.frgmt.my.impls.SystemMgr;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.ReadJsonFile;

import java.util.HashMap;

public class UserInfo {
    // 机顶盒账号
    private String userName;
    // 机顶盒MAC
    private String mac;
    // 机顶盒设备类型
    private String deviceType = "STB";
    // 机顶盒品牌
    private String brand;
    // 机顶盒型号
    private String model;
    // 站点编码
    private String siteCode;
    // 区域ID
    private String groupingID;

    // 机顶盒类型 unicast = 单播 Multicast = 组播
    private String boxType;

    // 设备终端号
    private String terminalNo;

    // 是否在黑/白名单中 None = 无  White = 白名单 Black = 黑名单
    private String inList;

    // 用户状态 normal = 正常 accountCancellation = 销户 downTime = 停机
    private String status;

    public String getBoxType() {
        return boxType;
    }

    public String getTerminalNo() {
        return terminalNo;
    }

    public String getInList() {
        return inList;
    }
    // 该用户是否是一个黑名单用户
    public boolean isUserBlack(){
        return "Black".equals(this.inList);
    }

    // 该用户是否是一个白名单用户
    public boolean isUserWhite(){
        return "White".equals(this.inList);
    }

    // 该用户状态是否正常
    public boolean isNormal(){
        return "normal".equals(this.status);
    }

    public String getStatus() {
        return status;
    }

    public String getGroupingID() {
        return groupingID;
    }

    // 登录的tokenCode
    private String tokenCode = null;
    // appKey
    private String appKey;

    // 私有构造函数
    private UserInfo(String userName, String mac, String deviceType,
                     String brand, String model, String siteCode, String tokenCode, String appKey, String groupingID) {
        this.userName = userName;
        this.mac = mac;
        this.deviceType = deviceType;
        this.brand = brand;
        this.model = model;
        this.siteCode = siteCode;
        this.tokenCode = tokenCode;
        this.appKey = appKey;
        this.groupingID = groupingID;
    }

    private static UserInfo _curUserInfo = null;

    public static void setLoginInfo(LoginData data) {
        UserInfo user = curUserInfo();
        user.siteCode = data.getSiteCode();
        user.groupingID = data.getGroupingID();
        user.tokenCode = data.getTokenCode();
        user.siteCode = data.getSiteCode();
        user.mac = data.getMac();
        user.status = data.getStatus();
        user.terminalNo = data.getTerminalNo();
        user.boxType = data.getBoxType();
        user.inList = data.getInList();
        // 需要将结果写入文件
        user.serializeToFile();
        AppLog.getCurLog().out("保存登录结果到文件成功");
    }

    // 构造当前的机顶盒账号对象
    public static BuildRes buildUserInfo(){
        try {
            boolean isFromFileUserInfo = true;
            _curUserInfo = unSerializeToFile();
            if (_curUserInfo == null) {
                isFromFileUserInfo = false;
                if (AppConstant.isTvStb()) {
                    // 如果是一个电视设备
                    _curUserInfo = new UserInfo("837012161053@HAITV", "585FF6273B05",
                            "STB", "Androidhuawei", "3.0", "telecomS",
                            "837012161053@HAITV_585FF6273B05", "appKey", "ff08869e-f0ce-4364-a739-99fe81297e83");
                } else {
                    // 在机顶盒上运行 则读取系统信息
                    SystemInfo systemInfo = SystemMgr.build();
                    _curUserInfo = new UserInfo(systemInfo.getAccountInfo(), systemInfo.getMac(), systemInfo.getDeviceType(),
                            systemInfo.getManufacturer(), systemInfo.getModel(), systemInfo.getSiteCode(),
                            systemInfo.getTokenCode(), systemInfo.getAppId(),
                            systemInfo.getGroupingId());
                }
                // 验证机顶盒账号 mac 等信息不能为空
                /////////////////////////////////
            }
            return new BuildRes(_curUserInfo,isFromFileUserInfo);
        }
        catch (Exception e){
            e.printStackTrace();
            return new BuildRes(e);
        }
    }

    // 获得当前的机顶盒用户
    public static UserInfo curUserInfo() {
        return _curUserInfo;
    }

    public static UserInfo setUserInfo(LoginData loginData) {
        UserInfo user = curUserInfo();
        user.groupingID = loginData.getGroupingID();
        user.tokenCode = loginData.getTokenCode();
        user.siteCode = loginData.getSiteCode();
        user.mac = loginData.getMac();
        user.status = loginData.getStatus();
        user.terminalNo = loginData.getTerminalNo();
        user.boxType = loginData.getBoxType();
        user.inList = loginData.getInList();

        _curUserInfo = user;
        // 需要将结果写入文件
        user.serializeToFile();
        return _curUserInfo;
    }
    public static UserInfo defUserInfo(){
        SystemInfo systemInfo = SystemMgr.build();
        return new UserInfo(systemInfo.getAccountInfo(), systemInfo.getMac(),systemInfo.getDeviceType(),
                systemInfo.getManufacturer(), systemInfo.getModel(), systemInfo.getSiteCode(),
                systemInfo.getTokenCode(), systemInfo.getAppId(),
                systemInfo.getGroupingId());
    }

    // 获得当前的机顶盒用户
//    public static UserInfo curUserInfo() {
//        if (_curUserInfo == null) {
//            // 837012161053@HAITV
//            _curUserInfo = new UserInfo("837012161053@HAITV", "585FF6273B05",
//                    "STB", "Androidhuawei", "3.0", "telecomS",
//                    "837012161053@HAITV_585FF6273B05", "appKey", "ff08869e-f0ce-4364-a739-99fe81297e83");
////            _curUserInfo = new UserInfo("dxrmcs09", "7497812EC357",
////                    "STB", "Androidhuawei", "3.0", "telecomS",
////                    "837012161053@HAITV_7497812EC357", "appKey", "ff08869e-f0ce-4364-a739-99fe81297e83");
//        }
//        return _curUserInfo;
//    }

    // 机顶盒账号
    public String getUserName() {
        return this.userName;
    }

    // 机顶盒MAC
    public String getMac() {
        return this.mac;
    }

    // 获取机设备类型
    public String getDeviceType() {
        return this.deviceType;
    }

    // 机顶盒品牌
    public String getBrand() {
        return this.brand;
    }

    // 机顶盒型号
    public String getModel() {
        return this.model;
    }

    // 获取当前网站编码
    public String getSiteCode() {
        return this.siteCode;
    }

    // tokenCode
    public String getTokenCode() {
        return this.tokenCode;
    }

    // appKey
    public String getAppKey() {
        return appKey;
    }

    public String toJson() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    // 序列化对象到文件
    public void serializeToFile(){
        String json = this.toJson();
        ReadJsonFile.writeFile("user.json",json);
    }

    // 反序列化成对象
    public static UserInfo unSerializeToFile(){
        UserInfo userInfo =null;
        try {
            String json = ReadJsonFile.readFile("user.json");
             userInfo = new Gson().fromJson(json, UserInfo.class);
        }catch (Exception e){
        }
        return userInfo;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "userName='" + userName + '\'' +
                ", mac='" + mac + '\'' +
                ", deviceType='" + deviceType + '\'' +
                ", brand='" + brand + '\'' +
                ", model='" + model + '\'' +
                ", siteCode='" + siteCode + '\'' +
                ", groupingID='" + groupingID + '\'' +
                ", boxType='" + boxType + '\'' +
                ", terminalNo='" + terminalNo + '\'' +
                ", inList='" + inList + '\'' +
                ", status='" + status + '\'' +
                ", tokenCode='" + tokenCode + '\'' +
                ", appKey='" + appKey + '\'' +
                '}';
    }

    public HashMap<String,String> toMap(){
        HashMap<String,String> dic = new HashMap<String,String>();
        dic.put("userName",this.userName);
        dic.put("mac",this.mac);
        dic.put("deviceType",this.deviceType);
        dic.put("brand",this.brand);
        dic.put("model",this.model);
        dic.put("siteCode",this.siteCode);
        dic.put("appKey",this.appKey);
        dic.put("groupingID",this.groupingID);

        return dic;
    }
}
